package com.sbck.cs3.fragment;

import android.Manifest;
import android.app.Fragment;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.sbck.cs3.MyGlobal;
import com.sbck.cs3.R;

public class MapFragment extends Fragment {


    View view;
    private MapView mapView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        this.view = inflater.inflate(R.layout.fragment_map, container, false);

        //检查权限
        if(ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 3);
        } else {
            initSDK(true);
            //获取地图控件引用
            mapView = (MapView) view.findViewById(R.id.mapView);

        }

        // Inflate the layout for this fragment
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        this.view = view;

        mapView = (MapView) view.findViewById(R.id.mapView);
        LatLng point = null;
        if((MyGlobal.unitstatus.WD > 30) && (MyGlobal.unitstatus.JD > 108))
        {
            point = new LatLng(MyGlobal.unitstatus.WD,MyGlobal.unitstatus.JD);
        }
        else {
            point = new LatLng(34.264369,108.815451);
        }

        BitmapDescriptor bitmapDescriptor= BitmapDescriptorFactory.fromResource(R.drawable.rqp_tm_small_small);

        OverlayOptions options = new MarkerOptions().position(point).icon(bitmapDescriptor);

        BaiduMap baiduMap = mapView.getMap();
        baiduMap.addOverlay(options);
        MapStatusUpdate mapStatusUpdate= MapStatusUpdateFactory.newLatLng( point);

        baiduMap.setMapStatus(mapStatusUpdate);
        point = new LatLng(point.latitude + 0.000001,point.longitude);
        baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng( point));

    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1:
                if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(getActivity(), "没有WRITE_EXTERNAL_STORAGE权限！", Toast.LENGTH_LONG).show();

                }
                break;
        }
        initSDK(true);
        //获取地图控件引用
        mapView = (MapView) view.findViewById(R.id.mapView);
    }
    private void initSDK(boolean status) {
        SDKInitializer.setAgreePrivacy(getActivity().getApplicationContext(), status);
        try {
            //在使用SDK各组件之前初始化context信息，传入ApplicationContext
            SDKInitializer.initialize(getActivity().getApplicationContext());
            //自4.3.0起，百度地图SDK所有接口均支持百度坐标和国测局坐标，用此方法设置您使用的坐标类型.
            //包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
            SDKInitializer.setCoordType(CoordType.BD09LL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mapView.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mapView.onPause();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mapView.onDestroy();
        mapView = null;
    }
}